//
//  ChatViewController.m
//  shusheng
//
//  Created by rimi on 16/7/11.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "ChatViewController.h"

#import <AVOSCloud/AVOSCloud.h>
#import <AVOSCloudIM/AVOSCloudIM.h>

@interface ChatViewController ()<UITableViewDataSource,UITableViewDelegate,AVIMClientDelegate>
- (void)initializeUserInterface; /** < 初始化用户界面 */
@property(nonatomic,strong) UITableView *chatTableView; // 聊天
@property (nonatomic, strong) AVIMClient *client;



@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeUserInterface];
//    [self tomSendMessageToJerry];
    [self jerryReceiveMessageFromTom];
}

- (void)initializeUserInterface{
    [self.view addSubview:self.chatTableView];
    
    [self.chatTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.bottom.equalTo(self.view).with.offset(-100);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    
}

#pragma mark -- 创建回话
- (void)tomSendMessageToJerry {
    // Tom 创建了一个 client，用自己的名字作为 clientId
    self.client = [[AVIMClient alloc] initWithClientId:@"Tom"];
    
    // Tom 打开 client
    [self.client openWithCallback:^(BOOL succeeded, NSError *error) {
        // Tom 建立了与 Jerry 的会话
        [self.client createConversationWithName:@"猫和老鼠" clientIds:@[@"Jerry"] callback:^(AVIMConversation *conversation, NSError *error) {
            // Tom 发了一条消息给 Jerry
            [conversation sendMessage:[AVIMTextMessage messageWithText:@"耗子，起床！" attributes:nil] callback:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"发送成功！");
                }
            }];
        }];
    }];
}

- (void)jerryReceiveMessageFromTom {
    // Jerry 创建了一个 client，用自己的名字作为 clientId
    self.client = [[AVIMClient alloc] initWithClientId:@"Jerry"];
    
    // 设置 client 的 delegate，并实现 delegate 方法
    self.client.delegate = self;
    
    // Jerry 打开 client
    [self.client openWithCallback:^(BOOL succeeded, NSError *error) {
        // ...
    }];
}

#pragma mark - AVIMClientDelegate

// 接收消息的回调函数
- (void)conversation:(AVIMConversation *)conversation didReceiveTypedMessage:(AVIMTypedMessage *)message {
    NSLog(@"%@", message.text); // 耗子，起床！
}

#pragma mark -- Getter

- (UITableView *)chatTableView{
    if (!_chatTableView) {
        _chatTableView = [[UITableView alloc] init];
        _chatTableView.dataSource = self;
        _chatTableView.delegate = self;
    }
    return _chatTableView;
}


#pragma mark -- UITableViewDataSource && UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}


#pragma mark -- Action
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}


@end
